package ch.christianschweizer.apps.ereservation.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;

@Entity
public class Inbox implements Identifiable, Serializable {

	public static class InboxBuilder {

		private String name;
		private InboxType inboxType;

		public InboxBuilder name(String name) {
			this.name = name;
			return this;
		}

		public InboxBuilder inboxType(InboxType inboxType) {
			this.inboxType = inboxType;
			return this;
		}

		public Inbox build() {
			Inbox inbox = new Inbox(this);
			inbox.name = name;
			inbox.inboxType = inboxType;
			return inbox;
		}
	}

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;

	@Version
	private long version;

	@Basic
	@Column(unique = true)
	private String name;

	@Enumerated
	private InboxType inboxType;

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List<History> history;

	protected Inbox() {
	}

	private Inbox(InboxBuilder builder) {
		this();
	}

	@Override
	public Long getUniqueIdentifier() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public InboxType getInboxType() {
		return inboxType;
	}

	public void setInboxType(InboxType inboxType) {
		this.inboxType = inboxType;
	}

	public List<History> getHistory() {
		return history;
	}

	@Override
	public String toString() {
		return "Inbox [id=" + id + ", name=" + name + ", inboxType="
				+ inboxType + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((inboxType == null) ? 0 : inboxType.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Inbox other = (Inbox) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (inboxType != other.inboxType) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

}
